package com.laomo.lib_music;

import android.annotation.SuppressLint;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;


import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：获取音频数据工具类
 *
 * @author laomo
 * @date 2024/7/19 15:23
 */
public class MicUtil {
    static MicUtil instance;

    private boolean isRecord;

    private AudioRecord mAudioRecord;
    private int mBufferSize;

    private List<OnMicDataListener> onMicDataListeners = new ArrayList<>();

    @SuppressLint("MissingPermission")
    private MicUtil() {
        mBufferSize = AudioRecord.getMinBufferSize(
                44100,
                AudioFormat.CHANNEL_IN_MONO,//双声道
                AudioFormat.ENCODING_PCM_16BIT);

        Log.d("lsflsddf","mBufferSize:"+mBufferSize);

        mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                44100,
                AudioFormat.CHANNEL_IN_MONO,//双声道
                AudioFormat.ENCODING_PCM_16BIT,
                mBufferSize);
    }

    public static MicUtil getInstance() {
        if (instance == null) {
            synchronized (MicUtil.class) {
                if (instance == null) {
                    instance = new MicUtil();
                }
            }
        }
        return instance;
    }

    public void addOnMicDataListener(OnMicDataListener onMicDataListener) {
        if (onMicDataListener != null && !onMicDataListeners.contains(onMicDataListener)) {
            onMicDataListeners.add(onMicDataListener);
        }
    }

    public void removeOnMicDataListener(OnMicDataListener onMicDataListener) {
        onMicDataListeners.remove(onMicDataListener);
    }


    public void startRecord() {
        if (isRecord) {
            return;
        }
        isRecord = true;
        RecordThread recordThread = new RecordThread();
        new Thread(recordThread).start();
    }

    public void stopRecord() {
        isRecord = false;
    }

    class RecordThread implements Runnable {

        @Override
        public void run() {
            mAudioRecord.startRecording();
            byte[] bytes = new byte[mBufferSize];
            while (isRecord) {
                int ret = mAudioRecord.read(bytes, 0, bytes.length);
                //录音错误
                if (ret == AudioRecord.ERROR_BAD_VALUE) {
                    isRecord = false;
                }
                processPcmData(bytes, ret);
                /*for (OnMicDataListener onMicDataListener : onMicDataListeners) {
                    onMicDataListener.onData(bytes);
                }*/
            }
            mAudioRecord.stop();
        }
    }


    private void processPcmData(byte[] buffer, int length) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < buffer.length; i++) {
            list.add((int) buffer[i]);
        }

        int[] b = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            b[i] = list.get(i);
        }

        for (OnMicDataListener onMicDataListener : onMicDataListeners) {
            onMicDataListener.onData(b);
        }
    }



    public interface OnMicDataListener {
        void onData(int[] data);
    }
}
